Game rắn săn mồi bằng c# _ snake full code
16.750 lượt xem;
- Form1.cs
- project /
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Windows.Forms;
5
6 namespace Snake
7 {
8 public partial class Form1 : Form
9 {
10 private List<Circle> Snake = new List<Circle>();
11 private Circle food = new Circle();
12
13 public Form1()
14 {
15 InitializeComponent();
16
17 //Set settings to default
18 new Settings();
19
20 //Set game speed and start timer
21 gameTimer.Interval = 1000 / Settings.Speed;
22 gameTimer.Tick += UpdateScreen;
23 gameTimer.Start();
24
25 //Start New game
26 StartGame();
27 }
28
29 private void StartGame()
30 {
31 lblGameOver.Visible = false;
32 ptbGameOver.Visible = false;
33
34 //Set settings to default
35 new Settings();
36
37 //Create new player object
38 Snake.Clear();
39 Circle head = new Circle {X = 10, Y = 5};
40 Snake.Add(head);
41
42
43 lblScore.Text = Settings.Score.ToString();
44 GenerateFood();
45
46 }
47
48 //Place random food object
49 private void GenerateFood()
50 {
51 int maxXPos = pbCanvas.Size.Width / Settings.Width;
52 int maxYPos = pbCanvas.Size.Height / Settings.Height;
53
54 Random random = new Random();
55 food = new Circle {X = random.Next(0, maxXPos), Y = random.Next(0, maxYPos)};
56 }
57
58
59 private void UpdateScreen(object sender, EventArgs e)
60 {
61 //Check for Game Over
62 if (Settings.GameOver)
63 {
64 //Check if Enter is pressed
65 if (Input.KeyPressed(Keys.Enter))
66 {
67 StartGame();
68 }
69 }
70 else
71 {
72 if (Input.KeyPressed(Keys.Right) && Settings.direction != Direction.Left)
73 Settings.direction = Direction.Right;
74 else if (Input.KeyPressed(Keys.Left) && Settings.direction != Direction.Right)
75 Settings.direction = Direction.Left;
76 else if (Input.KeyPressed(Keys.Up) && Settings.direction != Direction.Down)
77 Settings.direction = Direction.Up;
78 else if (Input.KeyPressed(Keys.Down) && Settings.direction != Direction.Up)
79 Settings.direction = Direction.Down;
80
81 MovePlayer();
82 }
83
84 pbCanvas.Invalidate();
85
86 }
87
88 private void pbCanvas_Paint(object sender, PaintEventArgs e)
89 {
90 Graphics canvas = e.Graphics;
91
92 if (!Settings.GameOver)
93 {
94 //Set colour of snake
95
96 //Draw snake
97 for (int i = 0; i < Snake.Count; i++)
98 {
99 Brush snakeColour;
100 if (i == 0)
101 snakeColour = Brushes.Black; //Draw head Download source code tai Codefly.vn
102 else
103 snakeColour = Brushes.Green; //Rest of body
104
105 //Draw snake Download source code tai Codefly.vn
106 canvas.FillEllipse(snakeColour,
107 new Rectangle(Snake[i].X * Settings.Width,
108 Snake[i].Y * Settings.Height,
109 Settings.Width, Settings.Height));
110
111
112 //Vẽ mồi cho rắn ăn Download source code tai Codefly.vn
113 canvas.FillEllipse(Brushes.Red,
114 new Rectangle(food.X * Settings.Width,
115 food.Y * Settings.Height, Settings.Width, Settings.Height));
116
117 }
118 }
119 else
120 {
121 //string gameOver = "Game over \nĐiểm số: " + Settings.Score + "\nNhấn Enter để chơi!";
122 string gameOver = "Nhấn Enter để chơi!";
123 lblGameOver.Text = gameOver;
124 lblGameOver.Visible = true;
125
126 ptbGameOver.Visible = true;
127 }
128 }
129
130
131 private void MovePlayer()
132 {
133 for (int i = Snake.Count - 1; i >= 0; i--)
134 {
135 //Move head
136 if (i == 0)
137 {
138 switch (Settings.direction)
139 {
140 case Direction.Right:
141 Snake[i].X++;
142 break;
143 case Direction.Left:
144 Snake[i].X--;
145 break;
146 case Direction.Up:
147 Snake[i].Y--;
148 break;
149 case Direction.Down:
150 Snake[i].Y++;
151 break;
152 }
153
154
155 //Get maximum X and Y Pos
156 int maxXPos = pbCanvas.Size.Width / Settings.Width;
157 int maxYPos = pbCanvas.Size.Height / Settings.Height;
158
159 //Detect collission with game borders.
160 if (Snake[i].X < 0 || Snake[i].Y < 0
161 || Snake[i].X >= maxXPos || Snake[i].Y >= maxYPos)
162 {
163 Die();
164 }
165
166
167 //Detect collission with body
168 for (int j = 1; j < Snake.Count; j++)
169 {
170 if (Snake[i].X == Snake[j].X &&
171 Snake[i].Y == Snake[j].Y)
172 {
173 Die();
174 }
175 }
176
177 //Detect collision with food piece
178 if (Snake[0].X == food.X && Snake[0].Y == food.Y)
179 {
180 Eat();
181 }
182
183 }
184 else
185 {
186 //Move body
187 Snake[i].X = Snake[i - 1].X;
188 Snake[i].Y = Snake[i - 1].Y;
189 }
190 }
191 }
192
193 private void Form1_KeyDown(object sender, KeyEventArgs e)
194 {
195 Input.ChangeState(e.KeyCode, true);
196 }
197
198 private void Form1_KeyUp(object sender, KeyEventArgs e)
199 {
200 Input.ChangeState(e.KeyCode, false);
201 }
202
203 private void Eat()
204 {
205 //Add circle to body
206 Circle circle = new Circle
207 {
208 X = Snake[Snake.Count - 1].X,
209 Y = Snake[Snake.Count - 1].Y
210 };
211 Snake.Add(circle);
212
213 //Update Score
214 Settings.Score += Settings.Points;
215 lblScore.Text = Settings.Score.ToString();
216
217 GenerateFood();
218 }
219
220 private void Die()
221 {
222 Settings.GameOver = true;
223 }
224 }
225 }
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Windows.Forms;
5
6 namespace Snake
7 {
8 public partial class Form1 : Form
9 {
10 private List<Circle> Snake = new List<Circle>();
11 private Circle food = new Circle();
12
13 public Form1()
14 {
15 InitializeComponent();
16
17 //Set settings to default
18 new Settings();
19
20 //Set game speed and start timer
21 gameTimer.Interval = 1000 / Settings.Speed;
22 gameTimer.Tick += UpdateScreen;
23 gameTimer.Start();
24
25 //Start New game
26 StartGame();
27 }
28
29 private void StartGame()
30 {
31 lblGameOver.Visible = false;
32 ptbGameOver.Visible = false;
33
34 //Set settings to default
35 new Settings();
36
37 //Create new player object
38 Snake.Clear();
39 Circle head = new Circle {X = 10, Y = 5};
40 Snake.Add(head);
41
42
43 lblScore.Text = Settings.Score.ToString();
44 GenerateFood();
45
46 }
47
48 //Place random food object
49 private void GenerateFood()
50 {
51 int maxXPos = pbCanvas.Size.Width / Settings.Width;
52 int maxYPos = pbCanvas.Size.Height / Settings.Height;
53
54 Random random = new Random();
55 food = new Circle {X = random.Next(0, maxXPos), Y = random.Next(0, maxYPos)};
56 }
57
58
59 private void UpdateScreen(object sender, EventArgs e)
60 {
61 //Check for Game Over
62 if (Settings.GameOver)
63 {
64 //Check if Enter is pressed
65 if (Input.KeyPressed(Keys.Enter))
66 {
67 StartGame();
68 }
69 }
70 else
71 {
72 if (Input.KeyPressed(Keys.Right) && Settings.direction != Direction.Left)
73 Settings.direction = Direction.Right;
74 else if (Input.KeyPressed(Keys.Left) && Settings.direction != Direction.Right)
75 Settings.direction = Direction.Left;
76 else if (Input.KeyPressed(Keys.Up) && Settings.direction != Direction.Down)
77 Settings.direction = Direction.Up;
78 else if (Input.KeyPressed(Keys.Down) && Settings.direction != Direction.Up)
79 Settings.direction = Direction.Down;
80
81 MovePlayer();
82 }
83
84 pbCanvas.Invalidate();
85
86 }
87
88 private void pbCanvas_Paint(object sender, PaintEventArgs e)
89 {
90 Graphics canvas = e.Graphics;
91
92 if (!Settings.GameOver)
93 {
94 //Set colour of snake
95
96 //Draw snake
97 for (int i = 0; i < Snake.Count; i++)
98 {
99 Brush snakeColour;
100 if (i == 0)
101 snakeColour = Brushes.Black; //Draw head Download source code tai Codefly.vn
102 else
103 snakeColour = Brushes.Green; //Rest of body
104
105 //Draw snake Download source code tai Codefly.vn
106 canvas.FillEllipse(snakeColour,
107 new Rectangle(Snake[i].X * Settings.Width,
108 Snake[i].Y * Settings.Height,
109 Settings.Width, Settings.Height));
110
111
112 //Vẽ mồi cho rắn ăn Download source code tai Codefly.vn
113 canvas.FillEllipse(Brushes.Red,
114 new Rectangle(food.X * Settings.Width,
115 food.Y * Settings.Height, Settings.Width, Settings.Height));
116
117 }
118 }
119 else
120 {
121 //string gameOver = "Game over \nĐiểm số: " + Settings.Score + "\nNhấn Enter để chơi!";
122 string gameOver = "Nhấn Enter để chơi!";
123 lblGameOver.Text = gameOver;
124 lblGameOver.Visible = true;
125
126 ptbGameOver.Visible = true;
127 }
128 }
129
130
131 private void MovePlayer()
132 {
133 for (int i = Snake.Count - 1; i >= 0; i--)
134 {
135 //Move head
136 if (i == 0)
137 {
138 switch (Settings.direction)
139 {
140 case Direction.Right:
141 Snake[i].X++;
142 break;
143 case Direction.Left:
144 Snake[i].X--;
145 break;
146 case Direction.Up:
147 Snake[i].Y--;
148 break;
149 case Direction.Down:
150 Snake[i].Y++;
151 break;
152 }
153
154
155 //Get maximum X and Y Pos
156 int maxXPos = pbCanvas.Size.Width / Settings.Width;
157 int maxYPos = pbCanvas.Size.Height / Settings.Height;
158
159 //Detect collission with game borders.
160 if (Snake[i].X < 0 || Snake[i].Y < 0
161 || Snake[i].X >= maxXPos || Snake[i].Y >= maxYPos)
162 {
163 Die();
164 }
165
166
167 //Detect collission with body
168 for (int j = 1; j < Snake.Count; j++)
169 {
170 if (Snake[i].X == Snake[j].X &&
171 Snake[i].Y == Snake[j].Y)
172 {
173 Die();
174 }
175 }
176
177 //Detect collision with food piece
178 if (Snake[0].X == food.X && Snake[0].Y == food.Y)
179 {
180 Eat();
181 }
182
183 }
184 else
185 {
186 //Move body
187 Snake[i].X = Snake[i - 1].X;
188 Snake[i].Y = Snake[i - 1].Y;
189 }
190 }
191 }
192
193 private void Form1_KeyDown(object sender, KeyEventArgs e)
194 {
195 Input.ChangeState(e.KeyCode, true);
196 }
197
198 private void Form1_KeyUp(object sender, KeyEventArgs e)
199 {
200 Input.ChangeState(e.KeyCode, false);
201 }
202
203 private void Eat()
204 {
205 //Add circle to body
206 Circle circle = new Circle
207 {
208 X = Snake[Snake.Count - 1].X,
209 Y = Snake[Snake.Count - 1].Y
210 };
211 Snake.Add(circle);
212
213 //Update Score
214 Settings.Score += Settings.Points;
215 lblScore.Text = Settings.Score.ToString();
216
217 GenerateFood();
218 }
219
220 private void Die()
221 {
222 Settings.GameOver = true;
223 }
224 }
225 }